from EaseXML import *
from urllib2 import urlopen, URLError

class LanguagesXML(XMLObject):
    _name = 'Languages'
    _encoding = 'utf-8'
    Languages = ListNode('LanguageXML')
    
class LanguageXML(XMLObject):
    _name = 'Language'
    _encoding = 'utf-8'
    #_nodesOrder = ["name", "abbreviation", "id"]
    name = TextNode()
    abbreviation = TextNode()
    id = TextNode()

class Mirrors(XMLObject):
    Mirrors = ListNode('Mirror')
    
class Mirror(XMLObject):
    #_nodesOrder = ["id", "mirrorpath", "typemask"]
    id = TextNode()
    mirrorpath = TextNode()
    typemask = TextNode()

class ActorsXML(XMLObject):
    _name = 'Actors'
    _encoding = 'utf-8'
    Actors = ListNode('ActorXML')

class ActorXML(XMLObject):
    _name = 'Actor'
    _encoding = 'utf-8'

    id = TextNode()
    Image = TextNode(optional=True)
    Name = TextNode()
    Role = TextNode()
    SortOrder = TextNode()

class BannersXML(XMLObject):
    _name = 'Banners'
    _encoding = 'utf-8'
    _prettyprint = True

    Banners = ListNode('BannerXML')
    
class BannerXML(XMLObject):
    _name = 'Banner'
    _encoding = 'utf-8'
    _prettyprint = True

    id = TextNode()
    BannerPath = TextNode()
    BannerType = TextNode(optional=True)
    BannerType2 = TextNode(optional=True)
    Colors = TextNode(optional=True)
    Language = TextNode(default=u'en')
    SeriesName = TextNode(optional=True)
    ThumbnailPath = TextNode(optional=True)
    VignettePath = TextNode(optional=True)
    Season = TextNode(optional=True)

class SeriesSearch(XMLObject):
    _name = 'Series'
    seriesid = TextNode()
    language = TextNode()
    SeriesName = TextNode()
    banner = TextNode(optional=True)
    Overview = TextNode()
    FirstAired = TextNode(optional=True)
    IMDB_ID = TextNode(optional=True)
    zap2it_id = TextNode(optional=True)
    id = TextNode()

class SeriesSearchData(XMLObject):
    _name = 'Data'
    Series = ListNode('SeriesSearch')

class SeriesData(XMLObject):
    _name = 'Data'
    Series = ListNode('SeriesXML', optional=True)

class SeriesXML(XMLObject):
    _name = 'Series'
    #_nodesOrder = ["id", "Actors", "Airs_DayOfWeek", "Airs_Time", "ContentRating", "FirstAired", "Genre",
    #                "IMDB_ID", "Language", "Network", "Overview", "Rating", "Runtime", "SeriesID", "SeriesName",
    #                "Status", "banner", "fanart", "lastupdated", "zap2it_id"]
    id = TextNode()
    Actors = TextNode(optional=True)
    Airs_DayOfWeek = TextNode(optional=True)
    Airs_Time = TextNode(optional=True)
    ContentRating = TextNode(optional=True)
    FirstAired = TextNode(optional=True)
    Genre = TextNode(optional=True)
    IMDB_ID = TextNode(optional=True)
    Language = TextNode(default='en')
    Network = TextNode(optional=True)
    Overview = TextNode(optional=True)
    Rating = TextNode(optional=True)
    Runtime = TextNode(optional=True)
    SeriesID = TextNode()
    SeriesName = TextNode()
    Status = TextNode(optional=True)
    banner = TextNode(optional=True)
    fanart = TextNode(optional=True)
    lastupdated = TextNode(optional=True)
    poster = TextNode(optional=True)
    zap2it_id = TextNode(optional=True)
    
class Episode(XMLObject):
    _encoding = 'utf-8'
#    _nodesOrder = ["id",
#                "DVD_chapter",
#                "DVD_discid",
#                "DVD_episodenumber",
#                "DVD_season",
#                "Director",
#                "EpisodeName",
#                "EpisodeNumber",
#                "FirstAired",
#                "GuestStars",
#                "IMDB_ID",
#                "Language",
#                "Overview",
#                "ProductionCode",
#                "Rating",
#                "SeasonNumber",
#                "Writer",
#                "absolute_number",
#                "airsafter_season",
#                "airsbefore_episode",
#                "airsbefore_season",
#                "filename",
#                "lastupdated",
#                "seasonid",
#                "seriesid"]
    id = TextNode()
    DVD_chapter = TextNode(optional=True)
    DVD_discid = TextNode(optional=True)
    DVD_episodenumber = TextNode(optional=True)
    DVD_season = TextNode(optional=True)
    Director = TextNode(optional=True)
    EpisodeName = TextNode(optional=True)
    EpisodeNumber = TextNode(optional=True)
    FirstAired = TextNode(optional=True)
    GuestStars = TextNode(optional=True)
    IMDB_ID = TextNode(optional=True)
    EpImgFlag = TextNode(optional=True)
    Language = TextNode(default='en')
    Overview = TextNode(optional=True)
    ProductionCode = TextNode(optional=True)
    Rating = TextNode(optional=True)
    SeasonNumber = TextNode()
    Writer = TextNode(optional=True)
    absolute_number = TextNode(optional=True)
    airsafter_season = TextNode(optional=True)
    airsbefore_episode = TextNode(optional=True)
    airsbefore_season = TextNode(optional=True)
    filename = TextNode(optional=True)
    lastupdated = TextNode(optional=True)
    flagged = TextNode(optional=True)
    mirrorupdate = TextNode(optional=True)
    seasonid = TextNode()
    seriesid = TextNode()

class EpisodeData(XMLObject):
    _name = 'Data'
    _encoding = 'utf-8'
    _prettyprint = True
    Episode = ItemNode('Episode')

class SeriesFull(XMLObject):
    _name = 'Data'
    _encoding = 'utf-8'
    Series = ItemNode('SeriesXML')
    Episodes = ListNode('Episode')

class CurrentServerTime(XMLObject):
    _name = 'Items'
    Time = TextNode()

API_KEY = 'D63BC339F161AB8B'

if __name__ == "__main__":
    import pprint

    url = "http://thetvdb.com/api/D63BC339F161AB8B/series/80348/all/en.xml"
    XML = urlopen(url).read()
    #pprint.pprint(XML)
    #XML = open('en.xml', 'r').read()
    doc = SeriesFull.fromXml(XML)
    
    #pprint.pprint( doc.toDict() )
    pprint.pprint(doc.Series.toDict())
    for episode in doc.Episodes:
        pprint.pprint(episode.toDict())
        #break

